package com._54programer.demo.nio;


import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author SN
 * @date 2019/11/28
 * @description 使用ByteBuffer和FileChannel，将E:\a.txt文件中的输出到控制台
 */
public class Demo2 {

    public static void main(String[] args) throws Exception{
        //1.获取FileChannel
        File file = new File("E:\\a.txt");
        FileInputStream fis = new FileInputStream(file);
        FileChannel channel = fis.getChannel();

        //2.创建Buffer, 并把通道的数据读到到Buffer种
        ByteBuffer buffer = ByteBuffer.allocate((int)file.length());
        channel.read(buffer);

        //3.输出缓冲区的数据并关闭输入流
        System.out.println(new String(buffer.array()));
        fis.close();
    }

}
